//
//  TakeCarDetailVC.m
//  Zhiche-Carriage
//
//  Created by 王亚陆 on 16/7/7.
//  Copyright © 2016年 LeeBruce. All rights reserved.
//

#import "TakeCarDetailVC.h"
#import <Masonry.h>
#import "Common.h"
#import <MJRefresh.h>
#import "RootViewController.h"
#import "QiniuSDK.h"
#import "Common.h"
#import <UIImageView+WebCache.h>
@interface TakeCarDetailVC ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIView * nav;
    Common * com;
    UIScrollView * scroll;
    NSMutableDictionary * dataSouceDic;
    RootViewController * TabBar;

}
@property (nonatomic,strong) UIView * BackView;
@property (nonatomic,strong) UIView * backview1;
@property (nonatomic,strong) UIView * backview2;
@property (nonatomic,strong) UIView * backview3;
@property (nonatomic,strong) UIView * backview4;


@property (nonatomic,copy) NSString *token;
@property (nonatomic,strong) NSMutableArray *pictureArray;
@property (nonatomic) int numberBtn;


@end

@implementation TakeCarDetailVC
- (instancetype)init

{
    self = [super init];
    if (self) {
        
        
        self.orderID = [[NSString alloc]init];
        
    }
    return self;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    nav = [self createNav:@"提车"];
    [self.view addSubview:nav];
    com = [[Common alloc]init];
    dataSouceDic = [[NSMutableDictionary alloc]init];
    self.pictureArray = [NSMutableArray array];
    TabBar = [RootViewController defaultsTabBar];

    [self createScroll];
    [self loadData:extractdetail_Url];

}

-(void)viewWillAppear:(BOOL)animated{
    
    [TabBar setTabBarHidden:YES];
}

-(void)loadData:(NSString *)url{
    
    //    hud = [WKProgressHUD showInView:self.view withText:@"加载中" animated:YES];
    NSString * string = [NSString stringWithFormat:@"%@?id=%@",url,self.orderID];
    [Common requestWithUrlString:string contentType:@"application/json" finished:^(id responseObj){
        
        dataSouceDic = responseObj[@"data"];
        [self createUI:dataSouceDic];

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //            [hud dismiss:YES];
        });
        
    } failed:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //        [hud dismiss:YES];
    });
}

-(void)createScroll{
    
    scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, Main_Width,Main_height-64*kHeight)];
    scroll.backgroundColor = GrayColor;
    scroll.contentOffset = CGPointMake(0, 0);
    scroll.contentSize = CGSizeMake(Main_Width, Main_height*1.5);
    self.automaticallyAdjustsScrollViewInsets =NO;
    scroll.bounces = NO;
    //隐藏横向、纵向的滚动条
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;
    scroll.delegate = self;
    scroll.minimumZoomScale = 1.0;
    scroll.maximumZoomScale = 3.0;
    [self.view addSubview:scroll];
    
    [scroll mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(nav.mas_bottom).with.offset(0*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width, Main_height-64*kHeight));
    }];
    
    _BackView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Main_Width, Main_height*kHeight*1.5)];
    _BackView.backgroundColor = GrayColor;
    [scroll addSubview:_BackView];
}

//extractdetail_Url


-(void)createUI:(NSMutableDictionary*)dic{
    
    
    [self createView1:dic];
    [self createView2:dic];
    [self createView3:dic];
    [self createView4:dic];


    
}


-(void)createView1:(NSMutableDictionary*)dic{
    
    NSString * ordercode = [NSString stringWithFormat:@"运单号：%@",dic[@"orderCode"]];
    UILabel * label = [com createUIlabel:ordercode andFont:FontOfSize12 andColor:fontGrayColor];
    [_BackView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left).with.offset(18*kWidth);
        make.centerY.mas_equalTo(_BackView.mas_top).with.offset(21.5*kHeight);
    }];

    _backview1 = [[UIView alloc]init];
    _backview1.backgroundColor = WhiteColor;
    [_BackView addSubview:_backview1];
    [_backview1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_BackView.mas_left);
        make.top.mas_equalTo(_BackView.mas_top).with.offset(43*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 186*kHeight));
    }];
    
    
    UILabel * CarModel = [com createUIlabel:@"地址" andFont:FontOfSize14 andColor:BlackTitleColor];
    [_backview1 addSubview:CarModel];
    [CarModel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backview1.mas_left).with.offset(18*kWidth);
        make.centerY.mas_equalTo(_backview1.mas_top).with.offset(21.5*kHeight);
    }];
    
    UIView * viewline1 = [[UIView alloc]init];
    viewline1.backgroundColor = LineGrayColor;
    [_backview1 addSubview:viewline1];
    [viewline1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_backview1.mas_top).with.offset(43*kHeight);
        make.left.mas_equalTo(_backview1.mas_left);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
    }];
    
    UIView * viewSline = [[UIView alloc]init];
    viewSline.backgroundColor = LineGrayColor;
    [_backview1 addSubview:viewSline];
    
    UILabel * CityName = [com createUIlabel:[NSString stringWithFormat:@"%@",dic[@"departCityName"]] andFont:FontOfSize13 andColor:littleBlackColor];
    UILabel * StoreName = [com createUIlabel:[NSString stringWithFormat:@"%@",dic[@"departUnit"]] andFont:FontOfSize13 andColor:littleBlackColor];
    UILabel * AddressName = [com createUIlabel:[NSString stringWithFormat:@"%@",dic[@"departAddr"]] andFont:FontOfSize13 andColor:littleBlackColor];
    NSString * people = [NSString stringWithFormat:@"%@ %@",dic[@"departContact"],dic[@"departPhone"]];
    UILabel * PeopleName = [com createUIlabel:people andFont:FontOfSize13 andColor:littleBlackColor];
    UIImageView * SmallImage = [[UIImageView alloc]init];
    SmallImage.image = [UIImage imageNamed:@"Address_Cell_fache"];
    
    [_backview1 addSubview:CityName];
    [_backview1 addSubview:StoreName];
    [_backview1 addSubview:AddressName];
    [_backview1 addSubview:PeopleName];
    [_backview1 addSubview:SmallImage];
    [CityName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(SmallImage.mas_right).with.offset(14*kWidth);
        make.top.mas_equalTo(_backview1.mas_top).with.offset((16+43)*kHeight);
    }];
    [StoreName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(CityName.mas_right).with.offset(10*kWidth);
        make.centerY.mas_equalTo(CityName.mas_centerY);
    }];
    
    [SmallImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backview1.mas_left).with.offset(20*kWidth);
        make.centerY.mas_equalTo(_backview1.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(12*kWidth, 16*kHeight));
    }];
    [AddressName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(CityName.mas_left).with.offset(0);
        make.centerY.mas_equalTo(SmallImage.mas_centerY);
    }];
    [PeopleName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(CityName.mas_left).with.offset(0);
        make.top.mas_equalTo(_backview1.mas_top).with.offset((73+43)*kHeight);
    }];
    
    UIButton * iPhoneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    iPhoneBtn.backgroundColor = YellowColor;
    [iPhoneBtn setTitle:@"" forState:UIControlStateNormal];
    [iPhoneBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
    iPhoneBtn.layer.borderWidth = 2;
    iPhoneBtn.layer.cornerRadius = 13*kHeight;
    iPhoneBtn.layer.borderColor = BtnBorderColor.CGColor;
    [iPhoneBtn addTarget:self action:@selector(pressIphoneBtn:) forControlEvents:UIControlEventTouchUpInside];
    [_backview1 addSubview:iPhoneBtn];
    [iPhoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(PeopleName.mas_centerY);
        make.right.mas_equalTo(_backview1.mas_right).with.offset(-15*kWidth);
        make.size.mas_equalTo(CGSizeMake(26*kHeight, 26*kHeight));
    }];
    
    
    
    [viewSline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(CityName.mas_top);
        make.left.mas_equalTo(_backview1.mas_left).with.offset(40*kWidth);
        make.bottom.mas_equalTo(PeopleName.mas_bottom);
        make.width.mas_equalTo(0.5);
    }];
    UIView * viewline11 = [[UIView alloc]init];
    viewline11.backgroundColor = LineGrayColor;
    [_backview1 addSubview:viewline11];
    [viewline11 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_backview1.mas_bottom).with.offset(-43*kHeight);
        make.left.mas_equalTo(_backview1.mas_left);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
    }];
    UIImageView * SmallImage1 = [[UIImageView alloc]init];
    SmallImage1.image = [UIImage imageNamed:@"Address_Cell_shouche"];
    [_backview1 addSubview:SmallImage1];
    [SmallImage1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backview1.mas_left).with.offset(20*kWidth);
        make.centerY.mas_equalTo(_backview1.mas_bottom).with.offset(-21.5*kHeight);
        make.size.mas_equalTo(CGSizeMake(12*kWidth, 16*kHeight));
    }];
    
    UILabel * CityName1 = [com createUIlabel:[NSString stringWithFormat:@"%@",dic[@"receiptCityName"]] andFont:FontOfSize13 andColor:littleBlackColor];
    UILabel * StoreName1 = [com createUIlabel:[NSString stringWithFormat:@"%@",dic[@"receiptUnit"]] andFont:FontOfSize13 andColor:littleBlackColor];
    [_backview1 addSubview:CityName1];
    [_backview1 addSubview:StoreName1];
    
    [CityName1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(SmallImage.mas_right).with.offset(14*kWidth);
        make.centerY.mas_equalTo(SmallImage1.mas_centerY);
    }];
    [StoreName1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(CityName1.mas_right).with.offset(10*kWidth);
        make.centerY.mas_equalTo(SmallImage1.mas_centerY);
    }];
    
    UIView * viewSline1 = [[UIView alloc]init];
    viewSline1.backgroundColor = LineGrayColor;
    [_backview1 addSubview:viewSline1];
    [viewSline1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_backview1.mas_left).with.offset(40*kWidth);
        make.size.mas_equalTo(CGSizeMake(0.5, 24*kHeight));
        make.centerY.mas_equalTo(_backview1.mas_bottom).with.offset(-21.5*kHeight);
    }];
    NSString * statueStr = [NSString stringWithFormat:@"%@",dic[@"statusText"]];
    UILabel * statue = [com createUIlabel:statueStr andFont:FontOfSize13 andColor:YellowColor];
    [_backview1 addSubview:statue];
    [statue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(_backview1.mas_right).with.offset(-15*kWidth);
        make.centerY.mas_equalTo(label.mas_centerY);
    }];

}

-(void)pressIphoneBtn:(UIButton*)sender{
    
    NSString * iphoneNumber = [NSString stringWithFormat:@"tel://%@",dataSouceDic[@"departPhone"]];
    
    UIWebView*callWebview =[[UIWebView alloc] init];
    NSURL *telURL =[NSURL URLWithString:iphoneNumber];// 貌似tel:// 或者 tel: 都行
    [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
    //记得添加到view上
    [self.view addSubview:callWebview];
    
}


-(void)createView2:(NSMutableDictionary*)dic{
    
    _backview2 = [[UIView alloc]init];
    _backview2.backgroundColor = WhiteColor;
    [_BackView addSubview:_backview2];
    [_backview2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(_backview1.mas_bottom).with.offset(10*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 129*kHeight));
    }];

    UILabel * timelabel = [com createUIlabel:@"时间" andFont:FontOfSize14 andColor:BlackTitleColor];
    [_backview2 addSubview:timelabel];
    [timelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backview2.mas_left).with.offset(18*kWidth);
        make.centerY.mas_equalTo(_backview2.mas_top).with.offset(21.5*kHeight);
    }];
    NSMutableArray * arr = [[NSMutableArray alloc]init];
    NSString * str1 = [NSString stringWithFormat:@"%@",dic[@"deliveryDate"]] ;
    [arr addObject:str1];
    
    NSString * str2 = [NSString stringWithFormat:@"%@",dic[@"arriveDate"]] ;
    [arr addObject:str2];
    
    NSArray * arrTime = @[@"提车时间:",@"送达时间:"];
    for (int i=0; i<arrTime.count; i++) {
        
        UIImageView * image = [[UIImageView alloc]init];
        [_backview2 addSubview:image];
        [image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_backview2.mas_left).with.offset(18*kWidth);
            make.centerY.equalTo(_backview2.mas_top).with.offset(64.5*kHeight+43*kHeight*i);
            make.size.mas_equalTo(CGSizeMake(17*kWidth, 13*kHeight));
        }];
        UILabel * time = [com createUIlabel:arrTime[i] andFont:FontOfSize13 andColor:fontGrayColor];
        [_backview2 addSubview:time];
        [time mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(image.mas_right).with.offset(10*kWidth);
            make.centerY.equalTo(_backview2.mas_top).with.offset(64.5*kHeight+43*kHeight*i);
        }];
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        
        [btn setTitleColor:littleBlackColor forState:UIControlStateNormal];
        btn.titleLabel.font = Font(FontOfSize13);
        [_backview2 addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(time.mas_right).with.offset(15*kWidth);
            make.centerY.equalTo(time.mas_centerY);
        }];
        UIImageView * imageline =[[UIImageView alloc]init];
        imageline.backgroundColor = GrayColor;
        
        if(i==1){
            imageline.tag = 50;
            image.image = [UIImage imageNamed:@"Address_Cell_songda"];
        }else{
            image.image = [UIImage imageNamed:@"Address_Cell_tiche"];
        }
        [_backview2 addSubview:imageline];
        [imageline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_backview2.mas_left).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
            make.top.mas_equalTo(_backview2.mas_top).with.offset(43*kHeight*(i+1));
        }];
    }

}

-(void)createView3:(NSMutableDictionary*)dic{
    
//                        amount = 1;
//                        brand =             {
//                            id = 2230;
//                            logo = "<null>";
//                        };
//                        brandName = "\U5b89\U51ef\U5ba2\U8f66";
//                        id = 3;
//                        vehicleName = "\U5b9d\U65af\U901a";
    
    NSMutableArray * vehicles = dic[@"vehicles"];
    int carNumber = vehicles.count;
    
    _backview3 = [[UIView alloc]init];
    _backview3.backgroundColor = WhiteColor;
    [_BackView addSubview:_backview3];
    [_backview3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(_backview2.mas_bottom).with.offset(10*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 86*kHeight+43*kHeight*carNumber));
    }];
    
    UILabel * carTytle = [com createUIlabel:@"车型" andFont:FontOfSize14 andColor:BlackTitleColor];
    [_backview3 addSubview:carTytle];
    [carTytle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backview3.mas_left).with.offset(18*kWidth);
        make.centerY.mas_equalTo(_backview3.mas_top).with.offset(21.5*kHeight);
    }];
    UIView * line1 = [[UIView alloc]init];
    line1.backgroundColor = GrayColor;
    [_backview3 addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(_backview3.mas_top).with.offset(43*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
    }];

    for (int i=0; i<carNumber; i++) {
        
        
        UIImageView * SmallImage = [[UIImageView alloc]init];
        
        NSURL * imageurl = [NSURL URLWithString:[NSString stringWithFormat:@"%@",vehicles[i][@"brand"][@"logo"]]];
        [SmallImage sd_setImageWithURL:imageurl placeholderImage:[UIImage imageNamed:@"car_bad"] options:SDWebImageCacheMemoryOnly];
        NSString * name = [NSString stringWithFormat:@"%@",vehicles[i][@"brandName"]];
        UILabel * CarName = [com createUIlabel:name andFont:FontOfSize13 andColor:BlackTitleColor];
        NSString * number = [NSString stringWithFormat:@"%@辆",vehicles[i][@"amount"]];
        UILabel * Number = [com createUIlabel:number andFont:FontOfSize13 andColor:BlackTitleColor];

        [_backview3 addSubview:SmallImage];
        [_backview3 addSubview:Number];
        [_backview3 addSubview:CarName];
        
        [SmallImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_backview3.mas_left).with.offset(18*kWidth);
            make.centerY.mas_equalTo(_backview3.mas_top).with.offset(43*kHeight+21.5*kHeight+43*i*kHeight);
            make.size.mas_equalTo(CGSizeMake(34*kWidth, 34*kHeight));
        }];
        
        [CarName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_backview3.mas_left).with.offset(54*kWidth);
            make.centerY.mas_equalTo(SmallImage.mas_centerY);
        }];
        
        [Number mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_backview3.mas_right).with.offset(-15*kWidth);
            make.centerY.mas_equalTo(SmallImage.mas_centerY);
        }];
        UIImageView * imageline =[[UIImageView alloc]init];
        imageline.backgroundColor = GrayColor;;
        [_backview3 addSubview:imageline];
        [imageline mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_backview3).with.offset(0);
            make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
            make.top.mas_equalTo(_backview3.mas_top).with.offset(86*kHeight+43*kHeight*carNumber);
        }];
        if (i==carNumber-1) {
            NSString * Number = [NSString stringWithFormat:@"合计：%d辆",carNumber];
            UILabel * amount =[com createUIlabel:Number andFont:FontOfSize14 andColor: YellowColor];
            [_backview3 addSubview:amount];
            [amount mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(_backview3.mas_right).with.offset(-15*kWidth);
                make.centerY.mas_equalTo(_backview3.mas_bottom).with.offset(-21.5*kHeight);
            }];
        }
    }
}

-(void)createView4:(NSMutableDictionary*)dic{
    
    _backview4 = [[UIView alloc]init];
    _backview4.backgroundColor = WhiteColor;
    [_BackView addSubview:_backview4];
    [_backview4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(_backview3.mas_bottom).with.offset(10*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width,139*kHeight));
    }];
    
    UILabel * carImage = [com createUIlabel:@"提车照片" andFont:FontOfSize14 andColor:BlackTitleColor];
    [_backview4 addSubview:carImage];
    [carImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backview4.mas_left).with.offset(18*kWidth);
        make.centerY.mas_equalTo(_backview4.mas_top).with.offset(21.5*kHeight);
    }];
    UIView * line1 = [[UIView alloc]init];
    line1.backgroundColor = GrayColor;
    [_backview4 addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(_backview4.mas_top).with.offset(43*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width, 0.5));
    }];

    UIButton * btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.backgroundColor = [UIColor cyanColor];
    [btn1 addTarget:self action:@selector(pressBtn1) forControlEvents:UIControlEventTouchUpInside];
    [_backview4 addSubview:btn1];
    btn1.tag = 601;
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_backview4.mas_left).with.offset(19*kWidth);
        make.top.equalTo(_backview4.mas_top).with.offset(55*kHeight);
        make.size.mas_equalTo(CGSizeMake(75*kWidth, 75*kHeight));
    }];
    UIButton * btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.backgroundColor = [UIColor cyanColor];
    [btn2 addTarget:self action:@selector(pressBtn2) forControlEvents:UIControlEventTouchUpInside];
    btn2.tag = 602;

    [_backview4 addSubview:btn2];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn1.mas_right).with.offset(14*kWidth);
        make.centerY.equalTo(btn1.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(75*kWidth, 75*kHeight));
    }];
    
    UIButton * pickCar = [UIButton buttonWithType:UIButtonTypeCustom];
    pickCar.backgroundColor = YellowColor;
    [pickCar setTitleColor:WhiteColor forState:UIControlStateNormal];
    [pickCar setTitle:@"确认提车" forState:UIControlStateNormal];
    pickCar.layer.cornerRadius = 5;
    [pickCar addTarget:self action:@selector(pressBtn3) forControlEvents:UIControlEventTouchUpInside];
    [_BackView addSubview:pickCar];
    [pickCar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(293*kWidth, 35*kHeight));
        make.top.mas_equalTo(_backview4.mas_bottom).with.offset(15*kHeight);
    }];
    
    NSMutableArray * vehicles = dic[@"vehicles"];
    int carNumber = vehicles.count-1;
    
    [_BackView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(Main_height*1.4+43*kHeight*carNumber);
        make.top.mas_equalTo(scroll.mas_top);
        make.left.mas_equalTo(scroll.mas_left);
        make.width.mas_equalTo(Main_Width);
    }];
    scroll.contentSize = CGSizeMake(Main_Width, Main_height*1.4+43*kHeight*carNumber);
    
}

-(void)pressBtn1{
    _numberBtn = 1;
    [self tapSelectPicture];
}
-(void)pressBtn2{
    _numberBtn = 2;

    [self tapSelectPicture];
}
-(void)pressBtn3{
    
    NSLog(@"确认提车");

}


//获取图片
-(void)tapSelectPicture
{
    __weak typeof(self) weakSelf = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [weakSelf getPictureFromCamera];
        
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"从相册选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf getPictureFromLibrary];
        
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    
    
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark 拍照
-(void)getPictureFromCamera
{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark 从相册选取
-(void)getPictureFromLibrary
{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:picker animated:YES completion:nil];
    
}


#pragma mark UIImagePickerControllerDelegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
        UIButton *button ;
        
        if (_numberBtn == 1) {
            
            button = (UIButton *)[self.view viewWithTag:601];
        } else {
            button = (UIButton *)[self.view viewWithTag:602];
        }
               
        [button setBackgroundImage:image forState:UIControlStateNormal];
        
        //上传服务器
        [self getTokenFromQNWithImage:image];
        
    }];
}


-(NSData *)getImageWith:(UIImage *)image
{
    NSData *data = nil;
    if (UIImagePNGRepresentation(image)) {
        
        data = UIImageJPEGRepresentation(image, 1);
        
        
    } else {
        data = UIImagePNGRepresentation(image);
        
        
    }
    
    return data;
}

#pragma mark 从七牛获取Token
-(void)getTokenFromQNWithImage:(UIImage *)image
{
    
    [Common requestWithUrlString:qiniu_token contentType:application_json finished:^(id responseObj) {
        if ([responseObj[@"success"] boolValue]) {
            
            
            self.token = responseObj[@"data"];
            
            NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
            
            [self uploadImageToQNWithData:[self getImageWith:image]];
        }
    } failed:^(NSString *errorMsg) {
        NSLog(@"%@",errorMsg);
    }];
    
    
    
}

-(void)uploadImageToQNWithData:(NSData *)data
{
    
    QNUploadManager *upManager = [[QNUploadManager alloc]init];
    
    QNUploadOption *uploadOption = [[QNUploadOption alloc]initWithMime:nil progressHandler:^(NSString *key, float percent) {
        
    } params:nil checkCrc:NO cancellationSignal:nil];
    
    [upManager putData:data key:nil token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        
        NSLog(@"%@",resp);
        
        NSDictionary *dic;
        
        if (_numberBtn == 1) {
            //提单照片
            dic = @{@"type":@"1010",
                    @"image":resp[@"key"]};
            
        } else {
            //提单现场照片
            dic = @{@"type":@"1020",
                    @"image":resp[@"key"]};
            
        }

        
        
        if (self.pictureArray.count > 0) {
            
            for (int i = 0; i < self.pictureArray.count; i++) {
                if ([self.pictureArray[i][@"type"] isEqualToString:dic[@"type"]]) {
                    [self.pictureArray removeObjectAtIndex:i];
                }
            }
            
            [self.pictureArray addObject:dic];
            
            
            
        } else {
            
            [self.pictureArray addObject:dic];
        }
        
        NSLog(@"%@",self.pictureArray);
        
        
    } option:uploadOption];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
